home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / bit / bit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-15  |  3.5 KB  |  156 lines

  1. /* 
  2.  * bit.c --
  3.  *
  4.  *    This file contains a program that exercises the bit
  5.  *    library procedures.  Invoke it with no parameters;  it
  6.  *    will print messages on stderr for any problems it detects
  7.  *    with the string procedures.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: /sprite/src/tests/bit/RCS/bit.c,v 1.1 88/06/19 17:53:41 ouster Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23. #include <stdio.h>
  24. #include <bit.h>
  25.  
  26. #define error(string) \
  27.     fprintf(stderr, string); \
  28.     exit(1);
  29.  
  30. /*
  31.  * Utility procedure to compare two arrays for equality.
  32.  */
  33.  
  34. void
  35. check(a1, a2, size, msg)
  36.     register int *a1;        /* First array. */
  37.     register int *a2;        /* Second array. */
  38.     int size;            /* No. of bits in each array. */
  39.     char *msg;            /* Message to print if there's a mismatch. */
  40. {
  41.     int i;
  42.  
  43.     for (i = 0; i < size; i++) {
  44.     if (Bit_Set(i, a1)) {
  45.         if (Bit_Set(i, a2)) {
  46.         continue;
  47.         }
  48.     } else {
  49.         if (Bit_Clear(i, a2)) {
  50.         continue;
  51.         }
  52.     }
  53.     error(msg);
  54.     }
  55. }
  56.  
  57. main()
  58. {
  59.     int *a1, *a2, *a3, i;
  60.     static int test1[] = {0xf0f0f0f0, 0xf0f0f0f0, 0xffff};
  61.     static int test2[] = {0x11111111, 0x88888888, 0x0000};
  62.     static int test3[] = {0x10101010, 0x80808080, 0x0000};
  63.     static int test4[] = {0xf1f1f1f1, 0xf8f8f8f8, 0xffff};
  64.  
  65.     /*
  66.      * Bit_Set, Bit_Clear, Bit_FindFirstSet
  67.      */
  68.  
  69.     Bit_Alloc(99, a1);
  70.     if (Bit_FindFirstSet(100, a1) != -1) {
  71.     error("Bit_FindFirstSet error 1\n");
  72.     }
  73.     Bit_Set(99, a1);
  74.     if (Bit_FindFirstSet(100, a1) != 99) {
  75.     error("Bit_FindFirstSet error 2\n");
  76.     }
  77.     Bit_Set(0, a1);
  78.     if (Bit_FindFirstSet(100, a1) != 0) {
  79.     error("Bit_FindFirstSet error 3\n");
  80.     }
  81.     Bit_Clear(0, a1);
  82.     if (Bit_FindFirstSet(100, a1) != 99) {
  83.     error("Bit_FindFirstSet error 4\n");
  84.     }
  85.     Bit_Clear(99, a1);
  86.     if (Bit_FindFirstSet(100, a1) != -1) {
  87.     error("Bit_FindFirstSet error 5\n");
  88.     }
  89.  
  90.     /*
  91.      * Bit_IsSet, Bit_IsClear
  92.      */
  93.  
  94.     Bit_Set(45, a1);
  95.     if (!Bit_IsSet(45, a1)) {
  96.     error("Bit_IsSet error 1\n");
  97.     }
  98.     if (Bit_IsClear(45, a1)) {
  99.     error("Bit_IsSet error 2\n");
  100.     }
  101.     if (Bit_IsSet(46, a1)) {
  102.     error("Bit_IsSet error 3\n");
  103.     }
  104.     if (!Bit_IsClear(46, a1)) {
  105.     error("Bit_IsSet error 4\n");
  106.     }
  107.     if (Bit_FindFirstSet(100, a1) != 45) {
  108.     error("Bit_IsSet error 5\n");
  109.     }
  110.  
  111.     /*
  112.      * Bit_FindFirstClear
  113.      */
  114.  
  115.     for (i = 0; i < 37; i++) {
  116.     Bit_Set(i, a1);
  117.     }
  118.     if (Bit_FindFirstClear(100, a1) != 37) {
  119.     error("Bit_FindFirstClear error 1\n");
  120.     }
  121.     Bit_Clear(36, a1);
  122.     if (Bit_FindFirstClear(100, a1) != 36) {
  123.     error("Bit_FindFirstClear error 2\n");
  124.     }
  125.  
  126.     /*
  127.      * Bit_AnySet, Bit_Zero
  128.      */
  129.  
  130.     if (!Bit_AnySet(100, a1)) {
  131.     error("Bit_AnySet error 1\n");
  132.     }
  133.     Bit_Zero(100, a1);
  134.     if (Bit_AnySet(100, a1)) {
  135.     error("Bit_AnySet error 2\n");
  136.     }
  137.  
  138.     /*
  139.      * Bit_Union, Bit_Intersect
  140.      */
  141.  
  142.     Bit_Intersect(72, test1, test2, a1);
  143.     check(a1, test3, 72, "Bit_Intersect error 1\n");
  144.     Bit_Union(72, test1, test2, a1);
  145.     check(a1, test4, 72, "Bit_Union error 1\n");
  146.  
  147.     /*
  148.      * Bit_Copy
  149.      */
  150.  
  151.     Bit_Copy(65, test1, a1);
  152.     check(test1, a1, 65, "Bit_Copy error 1\n");
  153.  
  154.     exit(0);
  155. }
  156.